#include "LvWindow.h"
#include <stdexcept>

LvWindow::LvWindow(int w, int h, std::string name)
	:width(w), height(h), windowName(name)
{
	initWindow();
}

LvWindow::~LvWindow()
{
	glfwDestroyWindow(window);
	glfwTerminate();
}

bool LvWindow::shouldClose()
{
	return glfwWindowShouldClose(window);
}

void LvWindow::createWindowSurface(VkInstance instance, VkSurfaceKHR* surface)
{
	if (glfwCreateWindowSurface(instance, window, nullptr, surface) != VK_SUCCESS)
	{
		throw std::runtime_error("failed to create window surface");
	}
}

void LvWindow::framebufferResizeCallback(GLFWwindow* window, int width, int height)
{
	auto lvWindow = reinterpret_cast<LvWindow*>(glfwGetWindowUserPointer(window));
	lvWindow->framebufferResized = true;
	lvWindow->width = width;
	lvWindow->height = height;
}

void LvWindow::initWindow()
{
	glfwInit();
	glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
	glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);


	window = glfwCreateWindow(width, height, windowName.c_str(), nullptr, nullptr);
	glfwSetWindowUserPointer(window, this);
	glfwSetFramebufferSizeCallback(window, framebufferResizeCallback);
}
